Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
Code is "skipped" when checking for collision

I'm createing a game for a school project, and I need to check when a player collides with a wall, but the function I've written only works for one side of the rectangle

function wallCollision(){
    if (player.x + player.l > wall.x && player.x < wall.x + wall.width){
        if (player.y == wall.y + wall.height){
            player.velocity.negy = 0;
        }
        if (player.y + player.l == wall.y){
            player.velocity.posy = 0;
        }
    }
    if (player.y < wall.y + wall.height && player.y + player.l > wall.y){
        if (player.x == wall.x + wall.width){
            player.velocity.negx = 0;
        }
        if (player.x + player.l == wall.x){
            player.velocity.posy = 0;
        }
    }
}

When I run it only works when checking id the player collided on the left side of the wall

if (player.x == wall.x + wall.width){
    player.velocity.negx = 0;
}

and when I tried to console.log inside the other 3 nested ifs I discovered that the code insade never runs

I've tried riarranging the function but it either didn't work completely or was somehow worse

neg and pos refer to the direction on the x and y axis, so posx = 0 means that the player cannot move right, negx = 0 that it cannot move let, posy = 0 that it cannot go downwards and negy = 0 that it cannot move upwards

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda